<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    :maskClosable="false"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="所属项目" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'projectCode', validatorRules.projectCode]" placeholder="请选择所属项目" @change="handleChangeProjectCode">
            <a-select-option v-for="item in projectList" :key="item.id" :value="item.code">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="所属路灯灯杆" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select  v-decorator="[ 'lightPoleCode', validatorRules.lightPoleCode]" placeholder="请选择所属路灯灯杆">
            <a-select-option v-for="item in lightPoleList" :key="item.id" :value="item.code">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input maxlength="20" v-decorator="[ 'name', validatorRules.name]" placeholder="请输入名称"></a-input>
        </a-form-item>

        <a-form-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input maxlength="20" v-decorator="[ 'code', validatorRules.code]" placeholder="请输入编号"></a-input>
        </a-form-item>

        <a-form-item label="物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag  v-decorator="['materialCode', validatorRules.materialCode]" :triggerChange="true" placeholder="请输入物料编码"
                              dictCode="glr_wled"/>
        </a-form-item>

        <a-form-item label="宽" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input  maxlength="5" v-decorator="[ 'width', validatorRules.width]" placeholder="请输入宽"></a-input>
        </a-form-item>

        <a-form-item label="高" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input maxlength="5" v-decorator="[ 'height', validatorRules.height]" placeholder="请输入高"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import {duplicateCheck } from '@/api/api'
  export default {
    name: "GlrLedModal",
    components: { 
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        confirmLoading: false,
        disableMixinCreated:true,
        lightPoleList:[],
        projectList:[],
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        validatorRules: {
          name: {rules: [
            {required: true, message: '请输入名称!'},
          ]},
          code: {rules: [
            {required: true, message: '请输入编号!'},
              {
                validator: this.validateLEDCode,
              }
          ]},
          projectCode: {rules: [
              {required: true, message: '请选择项目!'},
            ]},
          status: {rules: [
            {required: true, message: '请输入状态!'},
          ]},
          openStatus: {rules: [
          ]},
          lightPoleCode: {rules: [
          ]},
          gataWayCode: {rules: [
          ]},
          width: {rules: [
            {required: true, message: '请输入宽!'},
          ]},
          height: {rules: [
            {required: true, message: '请输入高!'},
          ]},
          brightness: {rules: [
          ]},
          progress: {rules: [
          ]},
          commandId: {rules: [
          ]},

        },
        url: {
          add: "/glr/glrLed/add",
          edit: "/glr/glrLed/edit",
          getLightPoleList: "/glr/glrLightPole/getLightPoleList",
          getProjectList: "/glr/glrProject/getProject",
        }
      }
    },
    created() {
    },
    methods: {
      validateLEDCode(rule, value, callback){
        var params = {
          tableName: 'glr_led',
          fieldName: 'code',
          fieldVal: value,
          dataId: this.model.id
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback("LED屏编号已存在!")
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        const that = this;
        httpAction(that.url.getProjectList,{},"post").then((res)=>{
          if(res.success){
            that.projectList=res.result;
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
        that.lightPoleList=[];
        that.model={};
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'lightPoleCode','projectCode','materialCode','name','code','status','openStatus','projectCode','lightPoleCode','gataWayCode','width','height','brightness','progress','commandId'))
        })
        if (this.model.projectCode){
          this.handleChangeProjectCode(this.model.projectCode);
        }
        if (this.model.companyCode){
          this.validateParam.companyCode=  this.model.companyCode;
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','code','status','openStatus','projectCode','lightPoleCode','gataWayCode','width','height','brightness','progress','commandId'))
      },
      handleChangeProjectCode(projectCode){
        const that = this;
        that.form.setFieldsValue({'lightPoleCode': ''});
        httpAction(that.url.getLightPoleList,{projectCode:projectCode,isWisdom:'Y'},"post").then((res)=>{
          if(res.success){
            that.lightPoleList=res.result;
            that.lightPoleList.unshift({id: "-1",code: "",name: "请选择"})
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      }
      
    }
  }
</script>